<template>
  <div>
    <el-form ref="form" :model="form" size="small" label-width="80px">
      <el-form-item label="分类名称">
        <el-input v-model="form.typeName"></el-input>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-switch v-model="form.enable" :active-value="1" :inactive-value="0" active-color="#13ce66">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toUpdate">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, toUpdate } from "@/api/type";

export default {
  props: {
    // 父组件传递的typeId
    activeId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {},
    };
  },
  watch: {
    activeId: {
      immediate: true,
      handler: function (newVal, oldVal) {
        this.getById();
      },
    },
  },
  methods: {
    // 根据id查询
    getById() {
      get(this.activeId).then((res) => {
        this.form = res.data;
      });
    },
    toUpdate() {
      toUpdate(this.form).then((res) => {
        this.$message.success(res.msg);
        this.$emit('after')
        this.$emit('closeDialog');
      });
    },
    closeDialog() {
      this.$emit("closeDialog");
    },
  },
};
</script>

<style>
</style>